import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 系统自带的线程池
 * 参数列表
 * 拒绝策略
 */
public class ThreadPoolExample2 {
    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3,
                5, 1,
                TimeUnit.SECONDS, new LinkedBlockingQueue<>(10));

        /**
         *     public ThreadPoolExecutor(int corePoolSize,                          : 核心线程数量
         *                               int maximumPoolSize,                       : 线程池中最大的线程数 (减去核心线程数量 = 临时线程)
         *                               long keepAliveTime,                        : 临时线程的存活时间
         *                               TimeUnit unit,                             : 临时线程存活的时间单位
         *                               BlockingQueue<Runnable> workQueue,         : 组织(保存)任务的队列
         *                               ThreadFactory threadFactory,               : 创建线程的工厂 (现在不关注)
         *                               RejectedExecutionHandler handler)          : 拒绝策略
         */

        // 拒绝策略
        // 直接拒绝
        ThreadPoolExecutor threadPoolExecutor1 = new ThreadPoolExecutor(3,
                5, 1,
                TimeUnit.SECONDS, new LinkedBlockingQueue<>(10),
                new ThreadPoolExecutor.AbortPolicy());

        // 放弃目前最早的任务
        ThreadPoolExecutor threadPoolExecutor2 = new ThreadPoolExecutor(3,
                5, 1, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(5),
                new ThreadPoolExecutor.DiscardOldestPolicy());

        // 返回新提交的任务
        ThreadPoolExecutor threadPoolExecutor3 = new ThreadPoolExecutor(3,
                5, 1, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(5),
                new ThreadPoolExecutor.DiscardPolicy());

        // 返回给调用者
        ThreadPoolExecutor threadPoolExecutor4 = new ThreadPoolExecutor(3,
                5, 1, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(5),
                new ThreadPoolExecutor.CallerRunsPolicy());

        for (int i = 0; i < 100; i++) {
            int taskId = i + 1;
            threadPoolExecutor1.submit(() -> {
                System.out.println("执行任务 : " +  taskId + " , " +Thread.currentThread().getName());
            });
        }
    }
}
